package com.han.hotelplat.comm.utils;

import java.util.ArrayList;
import java.util.List;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;

public class PushHelp {
	//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
    private static String appId = "MBeeQ3ow9A7dkmG5U9T4b6";
    private static String appKey = "kjMmUUCAtX5IxbyauwyeJ";
    private static String masterSecret = "Zg3xvMxqYW9iexVjOp3Tu5";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    private static long offlineExpireTime = 2*24*60*60*1000;//48小时
    
	private PushHelp() {
	}

	private static PushHelp instance = new PushHelp();

	public static PushHelp getInstance() {
		return instance;
	}
	
	/**
	 * 全局推送
		 * @description	
		 * @author  han1088
		 * @param title
		 * @param context
	 */
	public void pushMsg(String title, String context){
		IGtPush push = new IGtPush(url, appKey, masterSecret);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        LinkTemplate template = new LinkTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle(title);
        template.setText(context);
        template.setUrl("http://getui.com");

        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(offlineExpireTime);

        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
	}
	
	/**
	 * 
		 * @description指定客户端推送	
		 * @author  han1088
		 * @param title
		 * @param context
		 * @param cid 客户端id
	 */
	public void pushMsg(String title, String context, String cid){
		IGtPush push = new IGtPush(url, appKey, masterSecret);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        LinkTemplate template = new LinkTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle(title);
        template.setText(context);
        template.setUrl("http://getui.com");
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        SingleMessage message = new SingleMessage();
        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(offlineExpireTime);
        
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        
        IPushResult ret = push.pushMessageToSingle(message,target);
        System.out.println(ret.getResponse().toString());
	}
}
